package com.lxx.wiki.controller;

import com.lxx.wiki.domain.req.EbookPageQueryReq;
import com.lxx.wiki.domain.req.EbookQueryReq;
import com.lxx.wiki.domain.req.EbookSaveReq;
import com.lxx.wiki.domain.resp.CommonResp;
import com.lxx.wiki.domain.resp.EbookQueryResp;
import com.lxx.wiki.domain.resp.PageResp;
import com.lxx.wiki.service.EbookService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ebook")
public class EbookController {

    @Autowired
    private EbookService ebookService;

    /**
     * 分页查询ebook列表
     * 可根据name查询
     * @param
     * @return
     */
    @GetMapping("/list")
    public CommonResp list(@Valid EbookPageQueryReq ebookPageQueryReq) {
        PageResp<EbookQueryResp> ebookRespList = ebookService.list(ebookPageQueryReq);

        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        resp.setContent(ebookRespList);

        return resp;
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/all")
    public CommonResp all(EbookQueryReq ebookQueryReq) {
       List<EbookQueryResp> ebookRespList = ebookService.all(ebookQueryReq);

        CommonResp<List<EbookQueryResp>> resp = new CommonResp<>();
        resp.setContent(ebookRespList);

        return resp;
    }

    /**
     * 新增或修改ebook
     * @param ebookSaveReq
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody EbookSaveReq ebookSaveReq) {
        ebookService.save(ebookSaveReq);
        return new CommonResp<>();
    }

    /**
     * 根据id删除ebook
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public CommonResp del(@PathVariable Long id) {
        ebookService.del(id);
        return new CommonResp<>();
    }

}
